/**
 * Project: topdt-jbpm
 * Copyright 2013  Top Digital Technology  Co. Ltd.
 * All right reserved.
 */
package com.topdt.jbpm.dao;

import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.thx.common.orm.HibernateBaseDao;
import com.thx.common.util.AppContext;
import com.topdt.jbpm.model.TodoVar;

/**
 * 建立日期 : 2013-6-8 上午09:07:03<br>
 * 作者 : balance<br>
 * 模块 : <br>
 * 描述 : <br>
 * 修改历史: 序号 日期 修改人 修改原因 <br>
 * 1 <br>
 * 2 <br>
 */
@Repository
public class TodoVarDao extends HibernateBaseDao<TodoVar> {

	public void save(TodoVar var) {
		var.setInsertTime(new Date());
		super.save(var);
	}

	public void saveOrUpdate(TodoVar var) {
		var.setInsertTime(new Date());
		super.saveOrUpdate(var);
	}

	public void update(TodoVar var) {
		var.setInsertTime(new Date());
		super.update(var);
	}

	public TodoVar getOutCome(String todoId) {

		String hql = "from TodoVar where todoId=? and varType=?";

		List list = this.createQuery(hql, todoId, TodoVar.TYPE_OUTCOME).list();

		if (list != null && list.size() > 0)
			return (TodoVar) list.get(0);

		return null;
	}

	public void delOutCome(String todoId) {

		String hql = "delete from TodoVar where todoId=? and varType=?";
		this.createQuery(hql, todoId, TodoVar.TYPE_OUTCOME).executeUpdate();
	}

	public TodoVar getActor(String todoId) {

		String hql = "from TodoVar where todoId=? and varType=?";

		List list = this.createQuery(hql, todoId, TodoVar.TYPE_ACTOR).list();

		if (list != null && list.size() > 0)
			list.get(0);

		return null;
	}

	public void delActor(String todoId) {
		String hql = "delete from TodoVar where todoId=? and varType=?";
		this.createQuery(hql, todoId, TodoVar.TYPE_ACTOR).executeUpdate();
	}

	public void delByWorkId(String workId) {
		String comId = AppContext.getCurrentComId();
		String condition = "";
		if (comId != null && !"".equals(comId))
			condition = "companyId='" + comId + "' and ";

		String hql = "delete from TodoVar where " + condition + " workId=?";
		this.createQuery(hql, workId).executeUpdate();

	}

}
